from flask import Blueprint, session, redirect, render_template, request
from utils import db
from utils import cache

# 登录相关功能
# 蓝图对象
od = Blueprint('order', __name__)


@od.route('/order/stock', methods=['GET', 'POST'])
def order_list():
    # 从 GET 请求参数或 POST 表单中获取筛选条件
    classify = request.args.get("classify") or request.form.get("classify")
    name_s = request.args.get("name_s") or request.form.get("name_s")
    value_s = request.args.get("value_s") or request.form.get("value_s")
    encapsulation = request.args.get("encapsulation") or request.form.get("encapsulation")
    
    print("筛选条件:", classify, name_s, value_s, encapsulation)

    # 构建动态SQL查询
    sql = "select * from `入库表` where 1=1"
    params = []
    
    # 根据用户输入的筛选条件动态添加WHERE子句
    if classify:
        sql += " and 分类 like %s"
        params.append(f"%{classify}%")
    
    if name_s:
        sql += " and 名称 like %s"
        params.append(f"%{name_s}%")
    
    if value_s:
        sql += " and 值 like %s"
        params.append(f"%{value_s}%")
    
    if encapsulation:
        sql += " and 封装 like %s"
        params.append(f"%{encapsulation}%")
    
    # 执行查询
    data_list = db.fetch_all(sql, params)
    
    # 调试用:按照行打印数据
    # for row in data_list:
    #     print(row)
    
    # 将筛选条件传递给模板，以便在表单中保持用户输入
    return render_template('order_stock.html', 
                         data_list=data_list, 
                         classify=classify or '', 
                         name_s=name_s or '', 
                         value_s=value_s or '', 
                         encapsulation=encapsulation or '')


# "创建订单"
@od.route('/order/create', methods=['GET', 'POST'])
def order_create():
    if request.method == "GET":
        return render_template('order_create.html')

    url = request.form.get('url')
    count = request.form.get('count')

    # 写入数据库
    user_info = session.get('user_info')
    params = [url, count, user_info['id']]
    order_id = db.insert("insert into `order`(url, count,user_id,status) values (%s, %s,%s,1)", params)
    print(order_id)
    # 写入redis队列
    cache.push_queue(order_id)

    return redirect('/order/stock')


@od.route('/order/delete')
def delete_list():
    return "删除订单"


@od.route('/order/outbound', methods=['POST'])
def order_outbound():
    # 获取器件编号
    device_id = request.form.get('device_id')
    # 获取当前筛选条件
    classify = request.form.get('classify')
    name_s = request.form.get('name_s')
    value_s = request.form.get('value_s')
    encapsulation = request.form.get('encapsulation')
    
    # 打印器件编号和取出操作
    print(f"器件编号: {device_id}，执行取出操作")
    
    # 构建带筛选条件的重定向URL
    params = []
    if classify:
        params.append(f"classify={classify}")
    if name_s:
        params.append(f"name_s={name_s}")
    if value_s:
        params.append(f"value_s={value_s}")
    if encapsulation:
        params.append(f"encapsulation={encapsulation}")
    
    redirect_url = '/order/stock'
    if params:
        redirect_url += '?' + '&'.join(params)
    
    # 重定向回库存页面并保留筛选条件
    return redirect(redirect_url)
